package com.swallow.weixin.generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

/**
 * mybatis-plus 代码生成器
 * @author swallow
 */
public class CodeGenerator {

    public static void main(String[] args) {
//        1 new 一个代码生成器类
        AutoGenerator mpg = new AutoGenerator();
        final ResourceBundle rb = ResourceBundle.getBundle("mybatis-plus");

        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(rb.getString("outputDir"));
        gc.setOpen(false);
        gc.setFileOverride(true); //是否覆盖文件
        gc.setBaseResultMap(true);
        gc.setBaseColumnList(true);
        gc.setAuthor(rb.getString("author"));
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");
        gc.setServiceName("I%sService");
        gc.setServiceImplName("%sServiceImpl");
        gc.setControllerName("%sController");
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setUrl(rb.getString("url"));
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername(rb.getString("userName"));
        dsc.setPassword(rb.getString("password"));
        dsc.setTypeConvert(new MySqlTypeConvert(){
            @Override
            public IColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
                // 类型转换 默认 tnyint 转换成 short  这个技术人员使用 到底怎么改自己定义去 不想给你封装
                if(fieldType.toLowerCase().indexOf("tinyint")!=-1){
//                    fieldType = DbColumnType.BASE_SHORT.getType();
                    return DbColumnType.SHORT;
                }
                // 日期使用对应的 java.util.Date 类型不使用java.time.LocalTime
                if(fieldType.toLowerCase().equals("datetime")){
//                    fieldType = DbColumnType.DATE.getType();
                    return DbColumnType.DATE;
                }
                return super.processTypeConvert(globalConfig, fieldType);
            }
        });
        mpg.setDataSource(dsc);


        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(rb.getString("parent"));
        pc.setController(rb.getString("controller"));
        pc.setService(rb.getString("service"));
        pc.setServiceImpl(rb.getString("service") + ".impl");
        pc.setEntity(rb.getString("entity"));
        pc.setMapper(rb.getString("mapper"));
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return rb.getString("outputDirXml")  + "/" + tableInfo.getEntityName() + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        mpg.setTemplate(new TemplateConfig().setXml(null));

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setInclude(new String[]{rb.getString("tableName")});
        mpg.setStrategy(strategy);
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setController("/templates/mybatis/controller.java");
        templateConfig.setService("/templates/mybatis/service.java");
        templateConfig.setServiceImpl("/templates/mybatis/serviceImpl.java");
        templateConfig.setMapper("/templates/mybatis/mapper.java");
        templateConfig.setXml("/templates/mybatis/mapper.java");
        templateConfig.setEntity("/templates/mybatis/entity.java");
        templateConfig.setEntityKt("/templates/mybatis/enitty.kt");
        mpg.setTemplate(templateConfig);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }

}
